<template>
    <div class="model-tags" v-show="tags.length > 1">
        <div class="model-tags-button" @click="openDialog()">
            <a>Add</a>
        </div>
        <UxDialog :visible="visible" title="" @close="visible = false">
            <div class="tags-dialog">
                <svg-icon name="design-tag" />
                <h2>Select a mockup to preview your design</h2>
                <h3>
                    <span v-for="item in tags" :key="item" @click="setActive(item)" :class="{active:item==tag}" v-text="getTagName(item)"></span>
                </h3>

                <p v-show="tag=='hanging'">If your design is primarily centered, hanging mockups can create a more impactful effect.</p>
                <p v-show="tag=='flat lay'">Flat lay mockups are a good way to effectively showcase your design.</p>
                <p v-show="tag=='model'">Displaying your design on a model significantly enhances the overall presentation quality.</p>

                <div class="tags-dialog-model">
                    <div class="tags-dialog-model-visible" v-for="item in classify" :key="item.tag" :class="{active:item.model_tag == tag}">
                        <CommonTemplatePreview v-for="model in item.models" :key="model.tags" :baseModel="model" :baseColor="item.colors[0]" />
                    </div>
                </div>
                <button v-if="disabled" disabled>Added</button>
                <button v-else @click="setApply()" type="primary">Confirm</button>
            </div>
        </UxDialog>
    </div>
</template>

<script>
export default {
    props: {
        classify: { type: Array, default: () => [] },
    },
    data () {
        return {
            visible: false,
            tag: '',
        }
    },
    computed: {
        tags () {
            return this.classify.filter(c => c.models.length > 0).map(c => c.model_tag)
        },
        disabled () {
            const active = this.classify.find(c => c.model_tag == this.tag)
            return !active || active.show;
        }
    },

    methods: {

        getTagName (tag) {
            const active = this.classify.find(c => c.model_tag == tag) || { models: [] };
            const model = active.models[0] || {}
            return model.model_name || 'Hanging'


            // const list = new Map([
            //     ['hanging', 'Hanging'],
            //     ['flat lay', 'Flat lay'],
            //     ['model', 'Model'],
            // ])
            // return list.get(tag) || 'Hanging'
        },

        // 打开弹窗
        openDialog () {
            this.tag = this.tags[0]
            this.visible = true
        },

        // 选中标签
        setActive (name) {
            this.tag = name;
        },

        // 应用当前
        setApply () {
            this.$emit('select', this.tag)
            this.visible = false;
        },

    }
};
</script>

<style lang="scss" scoped>
.model-tags-button {
    & > button {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 130px;
        height: 28px;
        border-radius: 6px;
        border: 1px solid #c7c7c7;
        color: #555;
        font-size: 14px;
        font-family: Roboto-Medium, Roboto;
        font-weight: 500;

        svg {
            width: 16px;
            height: 16px;
            margin-left: 4px;
        }

        &:hover {
            color: #ea4c89;
        }
    }
}
.tags-dialog {
    width: 728px;
    max-height: 660px;
    height: 100vh;
    box-sizing: border-box;
    padding: 36px 24px 40px;
    display: flex;
    flex-direction: column;
    align-items: center;

    & > svg {
        width: 28px;
        height: 28px;
        min-height: 28px;
    }

    & > h2 {
        height: 18px;
        font-size: 16px;
        font-family: Roboto-Medium, Roboto;
        font-weight: 500;
        color: #000000;
        line-height: 18px;
        margin-top: 16px;
    }

    & > h3 {
        margin-top: 20px;
        height: 32px;
        box-sizing: border-box;
        padding: 2px;
        display: flex;
        background: #f0f0f0;
        border-radius: 6px;
        span {
            width: 116px;
            height: 28px;
            line-height: 28px;
            border-radius: 5px;
            font-size: 12px;
            font-family: Roboto-Medium, Roboto;
            font-weight: 500;
            color: #555555;
            text-align: center;
            cursor: pointer;

            & + span {
                margin-left: 2px;
            }

            &:hover {
                background-color: rgba(255, 255, 255, 0.6);
                color: #000;
            }

            &.active {
                background-color: #fff;
                color: #000;
            }
        }
    }

    & > p {
        margin-top: 20px;
        font-size: 12px;
        font-family: Roboto-Regular, Roboto;
        font-weight: 400;
        color: #555555;
        line-height: 18px;
    }

    .tags-dialog-model {
        display: flex;
        justify-content: center;
        border: 1px solid #f1f1f1;
        border-radius: 8px;
        margin-top: 20px;
        height: 340px;
        width: 680px;
        min-height: 340px;
        position: relative;
        .tags-dialog-model-visible {
            display: flex;
            opacity: 0;
            position: absolute;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            &.active {
                opacity: 1;
            }
        }

        .template-preview {
            width: 340px;
            height: 340px;
        }
    }

    & > a {
        margin-top: 14px;
        width: 100%;
        text-align: right;
        font-size: 12px;
        font-family: Roboto-Regular, Roboto;
        font-weight: 400;
        color: #3199bd;
        line-height: 18px;
        text-decoration: underline;
        cursor: pointer;
    }

    & > button {
        margin-top: 28px;
        width: 140px;
        height: 32px;
        min-height: 32px;
        border-radius: 6px;
        font-size: 14px;
        font-family: Roboto-Medium, Roboto;
        font-weight: 500;

        &[disabled] {
            background: #f4f9f7;
            border: none;
            color: #007a4c;
        }
    }
}

.tags-global {
    width: 320px;
    padding: 40px 20px 26px;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    align-items: center;
    figure {
        width: 80px;
        height: 80px;
        img {
            width: 100%;
            height: 100%;
        }
    }

    p {
        min-height: 170px;
        font-size: 14px;
        font-family: Roboto-Regular, Roboto;
        font-weight: 400;
        color: #000000;
        line-height: 22px;
        text-align: center;
        display: flex;
        justify-content: center;
        align-items: center;
    }
    fieldset {
        margin-top: 15px;
        width: 100%;
        display: flex;
        flex-direction: column;

        button {
            height: 38px;
            font-size: 14px;
            & + button {
                margin-top: 8px;
            }
        }
    }
}
</style>
